﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Kimd.Motion
{
    public class SensorLimit
    {
        public static Dictionary<string, Axis> MachineAxis;
        public static Dictionary<string, Sensor> MachineSensor;
        private Axis _thisAxis;
        public LimitType SensorLimitType { get; set; }
        public double SectionStartPosition { get; set; }
        public double SectionEndPosition { get; set; }
        public string LimitSensorName { get; set; }
        public string RelatedAxisName { get; set; }
        public double RelatedStartPosition { get; set; }
        public double RelatedEndPosition { get; set; }
        public bool IsSensed { get; set; }
        [XmlIgnore]
        public bool IsEnable { get; set; } = true;
        public SensorLimit()
        {

        }
        public void SetAxis(Axis axis)
        {
            this._thisAxis = axis;
        }
        public static void Init(Dictionary<string, Axis> machineAxis, Dictionary<string, Sensor> machineSensor)
        {
            MachineAxis = machineAxis;
            MachineSensor = machineSensor;
        }
        public bool IsBreakRule(bool moveDirection, out string errInfo)
        {
            errInfo = "";
            if (!IsEnable)
                return false;
            if (!string.IsNullOrEmpty(LimitSensorName))
            {
                if (!MachineSensor.ContainsKey(LimitSensorName))
                {
                    errInfo = "轴限制条件设置错误!限制感应器不存在";
                    return true;
                }
                if (string.IsNullOrEmpty(LimitSensorName))
                {
                    errInfo = "轴限制条件设置错误!限制感应器未设置";
                    return true;
                }
                if (!string.IsNullOrEmpty(RelatedAxisName) && MachineAxis.ContainsKey(RelatedAxisName))
                {
                    //当相关轴位置处于设定的区间内时
                    if (MachineAxis[RelatedAxisName].Position >= RelatedStartPosition && MachineAxis[RelatedAxisName].Position <= RelatedEndPosition)
                    {
                        //当前轴位置处于设定的区间内时
                        if (_thisAxis.Position >= SectionStartPosition && _thisAxis.Position <= SectionEndPosition)
                        {
                            //当前轴在位置下限附近，可以往负向移动
                            if (Math.Abs(_thisAxis.Position - SectionStartPosition) < 5 && !moveDirection)
                            {
                                return false;
                            }
                            //当前轴在位置上限附近，可以往正向移动
                            if (Math.Abs(_thisAxis.Position - SectionEndPosition) < 5 && moveDirection)
                            {
                                return false;
                            }
                            //感应器不达条件时时，限制起作用
                            switch (SensorLimitType)
                            {
                                case LimitType.SensorSensed:
                                    errInfo = $"感应器{LimitSensorName}感应到";
                                    return !MachineSensor[LimitSensorName].WaitOn();
                                case LimitType.SensorNotSensed:
                                    errInfo = $"感应器{LimitSensorName}未感应到";
                                    return MachineSensor[LimitSensorName].WaitOn();
                            }
                        }
                    }
                }
                else
                {
                    //当前轴位置处于设定的区间内时
                    if (_thisAxis.Position >= SectionStartPosition && _thisAxis.Position <= SectionEndPosition)
                    {
                        //当前轴在位置下限附近，可以往负向移动
                        if (Math.Abs(_thisAxis.Position - SectionStartPosition) < 5 && !moveDirection)
                        {
                            return false;
                        }
                        //当前轴在位置上限附近，可以往正向移动
                        if (Math.Abs(_thisAxis.Position - SectionEndPosition) < 5 && moveDirection)
                        {
                            return false;
                        }
                        //感应器不达条件时时，限制起作用
                        switch (SensorLimitType)
                        {
                            case LimitType.SensorSensed:
                                errInfo = $"感应器{LimitSensorName}感应到";
                                return !MachineSensor[LimitSensorName].WaitOn();
                            case LimitType.SensorNotSensed:
                                errInfo = $"感应器{LimitSensorName}未感应到";
                                return MachineSensor[LimitSensorName].WaitOn();
                        }
                    }
                    else
                        return false;
                }
            }
            return false;
        }
    }
}
